//
//  ReadListInfoViewController.m
//  Leisure
//
//  Created by 大展 on 16/6/30.
//  Copyright © 2016年 大展. All rights reserved.
//

#import "ReadListInfoViewController.h"

@interface ReadListInfoViewController ()<UIWebViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UIWebView *webView;
@property (nonatomic, strong)UITableView *tableView;

@end

@implementation ReadListInfoViewController


- (UITableView *)tableView {

    if (!_tableView) {

        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (UIWebView *)webView {

    if (!_webView) {

        _webView = [[UIWebView alloc]initWithFrame:self.view.frame];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
        _webView.scrollView.bouncesZoom = NO;
    }
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

//    [self.view addSubview:self.webView];
    [self.view addSubview:self.tableView];

    // 请求数据
    [RequestManager fecthDataWithUrl:KReadListInfoUrl parameter:@{@"contentid":self.contentID} mehtod:@"POST" compelet:^(NSData *data) {

        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",bigDic[@"data"][@"html"]);
        // 根据<pre>进行切割
        NSArray *array = [bigDic[@"data"][@"html"]  componentsSeparatedByString:@"<pre>"];
        [self.webView loadHTMLString:array[0] baseURL:nil];

    } updateUI:^{

    }];
}

#pragma mark -----webViewDelegate-----
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];

    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, kWidth, webView.scrollView.contentSize.height);
    webView.frame = CGRectMake(0, 0, kWidth, webView.scrollView.contentSize.height);
    self.tableView.tableHeaderView = webView;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"cell"];

    cell.textLabel.text = @"test";

    return cell;
}


- (void)webViewDidStartLoad:(UIWebView *)webView {

    NSLog(@"开始加载");

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
